<?php

namespace app\bll;

use Exception;
use support\Db;
use Throwable;

class UserLogregConfig
{

    /**
     * 查找登录注册的配置文件
     *
     * @return null|array
     */
    public function find(): null|array
    {
        $data =  Db::table('frame_user_logreg_config')
            ->where('id', '>', 0)
            ->first();
        return $data === null ? [] : get_object_vars($data);
    }

    /**
     * 判断是否存在
     *
     * @return boolean
     */
    public function exit(): bool
    {
        $info = $this->find();
        return $info  && count($info) > 0;
    }


    /**
     * 添加
     *
     * @param array $data
     * @return integer
     */
    public function add(array $data): int
    {
        return Db::table('frame_user_logreg_config')
            ->insertGetId($data);
    }

    /**
     * 编辑
     *
     * @param array $data
     * @param integer $id
     * @return integer
     */
    public function edit(array $data, int $id): int
    {
        return  Db::table('frame_user_logreg_config')
            ->where('id', $id)
            ->update($data);
    }

    /**
     * 保存自动判断编辑和新增
     *
     * @param array $data
     * @throws Throwable
     */
    public function save(array $data)
    {
        Db::beginTransaction();
        try {


            //修改注册字段表
            $phonerequire = 0;
            $emailrequire = 0;
            if (
                in_array(2, $data['logregtype'])
                || (isset($data['forgetpwd']) && in_array(1, $data['forgetpwd']))
                || $data['captchatype'] == 2
            ) {
                //手机免密 OR 手机验证 OR 手机找回密码 设置手机必填
                $phonerequire = 1;
            }

            if ((isset($data['forgetpwd'])  && in_array(2, $data['forgetpwd']))
                || $data['captchatype'] == 3
            ) {
                //  邮箱找回 设置邮箱必填
                $emailrequire = 1;
            }
            Db::table('frame_user_field')
                ->where('tablename', '=', 'frame_user')
                ->where('name', '=', 'phone')
                ->update([
                    'require' => $phonerequire,
                    'register' => $phonerequire
                ]);

            Db::table('frame_user_field')
                ->where('tablename', '=', 'frame_user')
                ->where('name', '=', 'email')
                ->update([
                    'require' => $emailrequire,
                    'register' => $emailrequire
                ]);


            $data['logregtype'] = is_array($data['logregtype']) ? implode(',', $data['logregtype'])
                : $data['logregtype'];


            $data['forgetpwd'] = isset($data['forgetpwd']) && is_array($data['forgetpwd']) ?
                implode(',', $data['forgetpwd'])
                : '';

            $origindata = $this->find();
            if (!$origindata) {
                $this->add($data);
            } else {
                $this->edit($data, $origindata['id']);
            }

            Db::commit();
        } catch (Throwable $ex) {
            Db::rollBack();
            throw $ex;
        }
    }
}
